package com.xiaobug.demo;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;

import com.xiaobug.baselibrary.base.BaseActivity;
import com.xiaobug.demo.alipay.ItemBean;
import com.xiaobug.demo.alipay.ItemMoveCallbackImpl;
import com.xiaobug.demo.alipay.ItemMoveHelperListener;
import com.xiaobug.demo.alipay.MenuItem;
import com.xiaobug.demo.alipay.adapter.MenuRecyclerviewGridAdapter;
import com.xiaobug.demo.databinding.DragLayoutRootBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by ljb on 2018/5/17.
 */

public class DragActivity extends BaseActivity<DragLayoutRootBinding> {

    private MenuRecyclerviewGridAdapter adapter;

    @Override
    protected int getRootID() {
        return R.layout.drag_layout_root;
    }

    @Override
    protected void initUI(Bundle savedInstanceState, DragLayoutRootBinding binding) {
        adapter = new MenuRecyclerviewGridAdapter(this);
        binding.rv.setAdapter(adapter);
        binding.rv.setLayoutManager(new GridLayoutManager(this, 4));
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemMoveCallbackImpl(new ItemMoveHelperListener() {
            @Override
            public void onItemMoved(int fromPosition, int toPosition) {
                Log.e("tag", "onItemMoved: " + fromPosition + "----------" + toPosition);
                if (fromPosition < toPosition) {
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(adapter.getData(), i, i + 1);
                    }
                } else {
                    for (int i = fromPosition; i > toPosition; i--) {
                        Collections.swap(adapter.getData(), i, i - 1);
                    }
                }
            }
        }));
        itemTouchHelper.attachToRecyclerView(binding.rv);
        adapter.setData(menuEditList);
    }

    List<ItemBean> menuItemList = new ArrayList<>();
    List<MenuItem> menuEditList = new ArrayList<>();

    {
        for (int i = 0; i < 50; i++) {
            List<MenuItem> menuItems = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                MenuItem menuItem = new MenuItem("groupId=" + j, "", "menu" + j, "菜单" + j);
                menuItems.add(menuItem);
            }
            ItemBean itemBean = new ItemBean("groupId=" + i, "分组" + i, menuItems);
            menuItemList.add(itemBean);
        }
        for (int i = 0; i < 40; i++) {
            MenuItem menuItem = new MenuItem("groupId=" + i, "", "menu" + i, "菜单" + i);
            menuEditList.add(menuItem);
        }
    }
}
